package org.oa.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.oa.common.WebConstant;
import org.oa.dao.OrganizationDao;
import org.oa.dao.UserDao;
import org.oa.dao.UserDataDao;
import org.oa.dao.impl.UserDaoImpl;
import org.oa.dto.Organization;
import org.oa.dto.User;
import org.oa.dto.UserData;
import org.oa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 用户服务实现类
 * @author Administrator
 *
 */
@Service
public class UserServiceImpl implements UserService {
	//以前的写法，需要自己创建对象
	//private UserDao userDao = new UserDaoImpl();
	//现在通过spring进行对象管理和注入
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private OrganizationDao organizationDao;
	@Autowired
	private UserDataDao userDataDao;
	
	public boolean save(User user) {
		userDao.save(user);
		return false;
	}

	public Map register(Organization org, User user, UserData userData) {
		//设置机构注册时间
		org.setRegisterTime(new Date());
		//持久化机构对象
		Long orgId = organizationDao.save(org);
		//设置用户与机构的关系
		user.setOrgId(orgId);
		//设置用户的注册时间
		user.setCreateTime(new Date());
		//设置用户的身份
		user.setPosition("2");
		//设置用户为有效用户
		user.setMark("1");
		//保存用户
		Long userId = userDao.save(user);
		if(userData == null){
			userData = new UserData();
		}
		//设置用户详细信息与机构的关系
		userData.setOrgId(orgId);
		//设置用户详细信息对象与用户基本信息对象的关系
		userData.setUserId(userId);
		//持久化用户详细信息对象
		userDataDao.save(userData);
		
		Map data = new HashMap();
		data.put(WebConstant.USER, user);
		data.put(WebConstant.ORG, org);
		
		return data;
	}

}
